//
//  ViewController.m
//  BleGPS
//
//  Created by 盈诺森 on 2019/12/6.
//  Copyright © 2019 盈诺森. All rights reserved.
//

#import "ViewController.h"
#import "Base_BlueTooth.h"
#import <CoreBluetooth/CoreBluetooth.h>
#import "BleGPS-Swift.h"
#import "Mld.pbobjc.h"

#define WeakObj(o) autoreleasepool{} __weak typeof(o) o##Weak = o;

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UILabel *labelconnect;
@property (weak, nonatomic) IBOutlet UILabel *DFU;

@property(nonatomic, strong)Base_BlueTooth *ble;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.ble = [[Base_BlueTooth alloc] init];
}


- (IBAction)button:(id)sender {
    [self.ble startScan];
    @WeakObj(self);
    self.ble.DeviceNameBlock = ^(NSArray *DeviceNameArray) {
        NSString *str = [DeviceNameArray componentsJoinedByString:@","];
        selfWeak.label.text = [NSString stringWithFormat:@"扫描返回:%@",str];
    };
}

- (IBAction)lianjie:(id)sender {
    @WeakObj(self);
    [self.ble centerDevice:@"0CA2964F-42F1-28D2-A437-35297705148D"];
    self.ble.connectionSucce = ^(NSString *connectionSucce) {
       selfWeak.labelconnect.text = [NSString stringWithFormat:@"%@",connectionSucce];
    };
    
    self.ble.deviceReturn = ^(NSData *deviceReturn) {
        selfWeak.label.text = [NSString stringWithFormat:@"设备返回:%@",[Device parseFromData:deviceReturn error:nil]];
    };
}

- (IBAction)duankai:(id)sender {
    [self.ble disconnect];
}

- (IBAction)发送:(id)sender {
    Server *dev = [[Server alloc] init];
    dev.devCtl.startStop = YES;
    NSData *data = [dev data];
    @WeakObj(self);
    [self.ble sendCommand:data];
    self.ble.deviceReturn = ^(NSData *deviceReturn) {
       NSString *string = [[NSString alloc] initWithData:deviceReturn encoding:NSUTF8StringEncoding];
        if ([string isEqualToString:@"OK"] || [string isEqualToString:@"FALSE"]) {
            NSLog(@"%@",string);
        } else {
            selfWeak.label.text = [NSString stringWithFormat:@"设备返回:%@",[Device parseFromData:deviceReturn error:nil]];
        }
    };
    
}

- (IBAction)shengji:(id)sender {
    NSLog(@"升级");
    @WeakObj(self);
    NSString *path = [[NSBundle mainBundle] pathForResource:@"mld_dfu_test_v200_" ofType:@"zip"];
    NSURL *url = [NSURL URLWithString:path];
    [self.ble initDFUService:url];
    self.ble.DFUReturn = ^(NSString *DFUReturn) {
        selfWeak.DFU.text = DFUReturn;
    };
}

- (IBAction)info:(id)sender {
    NSString *string = [self convertDecimalSystemFromBinarySystem:@"00000000010"];
    Server *dev = [[Server alloc] init];
    dev.devFun.function = (int)string;
    dev.devFun.functionTime = 1;
    NSData *data = [dev data];
    [self.ble sendCommand:data];
    @WeakObj(self);
    self.ble.deviceReturn = ^(NSData *deviceReturn) {
        NSString *string = [[NSString alloc] initWithData:deviceReturn encoding:NSUTF8StringEncoding];
        if ([string isEqualToString:@"OK"] || [string isEqualToString:@"FALSE"]) {
            NSLog(@"%@",string);
        } else {
            selfWeak.label.text = [NSString stringWithFormat:@"设备返回:%@",[Device parseFromData:deviceReturn error:nil]];
        }
    };
}
#pragma mark 二进制转十进制
- (NSString *)convertDecimalSystemFromBinarySystem:(NSString *)binary
{
    NSInteger ll = 0 ;
    NSInteger  temp = 0 ;
    for (NSInteger i = 0; i < binary.length; i ++){
        
        temp = [[binary substringWithRange:NSMakeRange(i, 1)] intValue];
        temp = temp * powf(2, binary.length - i - 1);
        ll += temp;
    }
    
    NSString * result = [NSString stringWithFormat:@"%ld",ll];
    
    return result;
}


@end
